package com.flower.controller.farmer;

import com.flower.utils.AjaxResult;
import com.flower.domain.dto.farmer.FlowerDetailDTO;
import com.flower.domain.entity.FlowerCulture;
import com.flower.service.FlowerCultureService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 花农端的花卉文化知识管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/flower")
public class FlowerCultureController {

    private final FlowerCultureService flowerCultureService;

    /**
     * 获取所有花卉文化知识
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getFlowerList() {
        List<FlowerDetailDTO> list = flowerCultureService.getFlowerList();
        return AjaxResult.success(list);
    }

    /**
     * 获取热门花卉
     * @return
     */
    @GetMapping("/popular")
    public AjaxResult getPopularFlowers() {
        List<FlowerDetailDTO> list = flowerCultureService.getFlowerList();
        // 按访问量排序，取前10个
        List<FlowerDetailDTO> popularList = list.stream()
                .sorted((a, b) -> b.getViewCount().compareTo(a.getViewCount()))
                .limit(10)
                .toList();
        return AjaxResult.success(popularList);
    }

    /**
     * 获取花卉文化知识详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getFlowerDetail(@PathVariable("id") Long id) {
        FlowerDetailDTO detail = flowerCultureService.getFlowerDetail(id);
        if (detail == null) {
            return AjaxResult.error("花卉信息不存在");
        }
        // 增加访问量
        flowerCultureService.incrementViewCount(id);
        return AjaxResult.success(detail);
    }

    /**
     * 保存或更新花卉文化知识
     * @param flowerCulture
     * @return
     */
    @PostMapping("/culture")
    public AjaxResult saveOrUpdateCulture(@RequestBody FlowerCulture flowerCulture) {
        try {
            boolean result = flowerCultureService.saveOrUpdateCulture(flowerCulture);
            if (result) {
                return AjaxResult.success("保存花卉文化知识成功");
            } else {
                return AjaxResult.error("保存花卉文化知识失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("保存花卉文化知识失败：" + e.getMessage());
        }
    }

    /**
     * 获取花卉文化知识
     * @param encyclopediaId
     * @return
     */
    @GetMapping("/culture/{encyclopediaId}")
    public AjaxResult getFlowerCulture(@PathVariable("encyclopediaId") Long encyclopediaId) {
        try {
            FlowerCulture culture = flowerCultureService.getFlowerCulture(encyclopediaId);
            return AjaxResult.success(culture);
        } catch (Exception e) {
            return AjaxResult.error("获取花卉文化知识失败：" + e.getMessage());
        }
    }

    /**
     * 搜索花卉文化知识
     * @param keyword
     * @return
     */
    @GetMapping("/search")
    public AjaxResult searchFlowers(@RequestParam("keyword") String keyword) {
        List<FlowerDetailDTO> list = flowerCultureService.searchFlowers(keyword);
        return AjaxResult.success(list);
    }
} 